package com.taotao.search.controller;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.taotao.common.ExceptionUtil;
import com.taotao.common.TaotaoResult;
import com.taotao.search.pojo.SearchResult;
import com.taotao.search.service.SearchService;

@Controller
public class SearchController {
	@Autowired
	private SearchService searchService;
	@RequestMapping(value="/query",method=RequestMethod.GET)
	@ResponseBody
	public TaotaoResult getItemsResult(@RequestParam(value="q") String query,
			@RequestParam(value="page",defaultValue="1") int indexPage,
			@RequestParam(value="rows",defaultValue="60") int rows){
		if(StringUtils.isBlank(query)){
			return TaotaoResult.build(400, "查询条件不能为空!");
		}
		SearchResult searchResult=null;
		try {
			query=new String(query.getBytes("iso8859-1"),"utf-8");
			searchResult = searchService.getItemsInfo(query, indexPage, rows);
		} catch (Exception e) {
			e.printStackTrace();
			return TaotaoResult.build(500, ExceptionUtil.getStackTrace(e));
		}
		return TaotaoResult.ok(searchResult);
	}
}
